package com.zj.syzs.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v4.util.LruCache;
import android.widget.ImageView;

import java.io.IOException;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class ImageDownLoader {
    private static final int LOAD_SUCCESS = 1;
    /**
     * ����Image���࣬���洢Image�Ĵ�С����LruCache�趨��ֵ��ϵͳ�Զ��ͷ��ڴ�
     */
    private LruCache<String, Bitmap> lruCache;
    /**
     * �ļ�����������
     */
    private FileUtils utils;

    private ThreadPoolExecutor executor;

    public ImageDownLoader(Context context) {
        super();
        // �����̳߳� ��С�߳���
        executor = new ThreadPoolExecutor(1, 4, 2, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());
        // ��ȡϵͳ�����Ӧ�ó��������ڴ�
        int maxMemory = (int) Runtime.getRuntime().maxMemory();
        int maxSize = maxMemory / 8;
        lruCache = new LruCache<String, Bitmap>(maxSize) {

            @Override
            protected int sizeOf(String key, Bitmap value) {
                // ����Bitmap�Ĵ�С Ĭ�Ϸ���ͼƬ����
                return value.getRowBytes() * value.getHeight();
            }
        };
        utils = new FileUtils(context);
    }

    /**
     * @return Bitmap ��������
     * @throws
     * @Title: downLoader
     * @˵ ��: ����ͼƬ
     * @�� ��: @param url
     * @�� ��: @param loaderlistener
     * @�� ��: @return
     */
    public Bitmap downLoader(final ImageView imageView, final ImageLoaderlistener loaderlistener) {
        final String url = (String) imageView.getTag();
        if (url != null) {
            final Bitmap bitmap = showCacheBitmap(url);
            if (bitmap != null) {
                return bitmap;
            } else {
                final Handler handler = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        loaderlistener.onImageLoader((Bitmap) msg.obj, imageView);
                    }
                };

                executor.execute(new Runnable() {

                    @Override
                    public void run() {
                        Bitmap bitmap = HttpUtils.getBitmapFormUrl(url);
                        if (bitmap != null) {
                            Message msg = handler.obtainMessage();
                            msg.obj = bitmap;
                            msg.what = LOAD_SUCCESS;
                            handler.sendMessage(msg);
                            try {
                                utils.savaBitmap(url, bitmap);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            lruCache.put(url, bitmap);
                        }
                    }
                });
            }
        }
        return null;
    }

    /**
     * @return Bitmap �������� ͼƬ
     * @throws
     * @Title: showCacheBitmap
     * @˵ ��: ��ȡbitmap���� : �ڴ���û�о�ȥsd����ȥ��
     * @�� ��: @param url ͼƬ��ַ
     * @�� ��: @return
     */
    public Bitmap showCacheBitmap(String url) {
        Bitmap bitmap = getMemoryBitmap(url);
        if (bitmap != null) {
            return bitmap;
        } else if (utils.isFileExists(url) && utils.getFileSize(url) > 0) {
            bitmap = utils.getBitmap(url);
            lruCache.put(url, bitmap);
            return bitmap;
        }
        return null;
    }

    /**
     * @return Bitmap ��������
     * @throws
     * @Title: getMemoryBitmap
     * @˵ ��:��ȡ�ڴ��е�ͼƬ
     * @�� ��: @param url
     * @�� ��: @return
     */
    private Bitmap getMemoryBitmap(String url) {
        return lruCache.get(url);
    }

    public interface ImageLoaderlistener {
        public void onImageLoader(Bitmap bitmap, ImageView imageView);
    }

    /**
     * @return void ��������
     * @throws
     * @Title: cancelTask
     * @˵ ��:ֹͣ���������߳�
     * @�� ��:
     */
    public void cancelTask() {
        if (executor != null) {
            executor.shutdownNow();
        }
    }

}
